# 获取目录下文件大小并计算文件大小总和
import os
import os.path


class FileSizeModel():
    def __init__(self, *args, **kw):
        super().__init__()
        self.filename = ""
        self.filename = ""
        print(args)
        print(kw)


def StrOfSize(size):
    '''
    auth: wangshengke@kedacom.com ；科达柯大侠
    递归实现，精确为最大单位值 + 小数点后三位
    '''
    def strofsize(integer, remainder, level):
        if integer >= 1024:
            remainder = integer % 1024
            integer //= 1024
            level += 1
            return strofsize(integer, remainder, level)
        else:
            return integer, remainder, level

    units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
    integer, remainder, level = strofsize(size, 0, 0)
    if level+1 > len(units):
        level = -1
    return ('{}.{:>03d} {}'.format(integer, remainder, units[level]))


def get_size(path, deep=2):
    if os.path.isfile(path):
        size = os.path.getsize(path)
        if deep > 0:
            print('f',  StrOfSize(size), path)
        return size
    else:
        fileList = os.listdir(path)  # 获取path目录下所有文件
        list1 = []
        for filename in fileList:
            pathTmp = os.path.join(path, filename)
            size = get_size(pathTmp, deep-1)
            list1.append(size)
        size = (sum(list1))
        if deep > 0:
            print('d', StrOfSize(size), path)
        return size
    return


get_size(r"D:\\workspace\\injoygo\\Public\\static\\injoy\\images")
